package HDFS;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Test_04 {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        Configuration conf = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata211:9000"), conf, "root");
        //获取文件迭代器类型（调用方法文件列表）
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/data"),true);
        //遍历迭代器
        //判断是否存在
        while (listFiles.hasNext()){
            //遍历输出
            //获取到一个文件状态信息
            LocatedFileStatus  fileStatus= listFiles.next();
            //打印信息
            //获取文件名
            String name = fileStatus.getPath().getName();
            System.out.println(name);
            //获取文件块大小
            System.out.println(fileStatus.getBlockSize());
            //获取权限
            System.out.println(fileStatus.getPermission());

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation bl : blockLocations) {
                System.out.println("块的偏移量"+bl.getOffset());
                System.out.println("块的名称"+bl.getNames());
                String[] hosts = bl.getHosts();
                for (String host : hosts) {
                    System.out.println("主机名"+host);

                }

            }
            System.out.println("--------------");


        }

    }
}
